static void free_blocks(struct stack_block *block)
{
struct stack_block *next;
+ int old_errno = errno;
/* free all the blocks, except if any of them is bigger than
unused_block, replace it */
block = next;
}
+ errno = old_errno;
}
#ifdef DEBUG
{
struct stack_block *block;
size_t prev_size, alloc_size;
+ int old_errno = errno;
prev_size = current_block == NULL ? 0 : current_block->size;
/* Use INITIAL_STACK_SIZE without growing it to nearest power. */
i_panic("data stack: Out of memory when allocating %zu bytes",
alloc_size + SIZEOF_MEMBLOCK);
}
+ errno = old_errno;
block->size = alloc_size;
block->canary = BLOCK_CANARY;
mem_block_reset(block);
void *ret;
size_t alloc_size;
bool warn = FALSE;
-#ifdef DEBUG
int old_errno = errno;
-#endif
if (unlikely(size == 0 || size > SSIZE_T_MAX))
i_panic("Trying to allocate %zu bytes", size);
current_block->left -= alloc_size;
if (warn) T_BEGIN {
- /* sending event can cause errno changes. */
-#ifdef DEBUG
- i_assert(errno == old_errno);
-#else
- int old_errno = errno;
-#endif
/* warn after allocation, so if e_debug() wants to
allocate more memory we don't go to infinite loop */
data_stack_send_grow_event(alloc_size);
- /* reset errno back to what it was */
- errno = old_errno;
} T_END;
#ifdef DEBUG
memcpy(ret, &size, sizeof(size));
had used t_buffer_get(). */
memset(PTR_OFFSET(ret, size), CLEAR_CHR,
MEM_ALIGN(size + SENTRY_COUNT) - size);
-
- /* we rely on errno not changing. it shouldn't. */
- i_assert(errno == old_errno);
#endif
+ errno = old_errno;
return ret;
}
void data_stack_free_unused(void)
{
+ int old_errno = errno;
free(unused_block);
unused_block = NULL;
+ errno = old_errno;
}
void data_stack_init(void)
pool_t pool_allocfree_create(const char *name ATTR_UNUSED)
{
struct allocfree_pool *pool;
+ int old_errno = errno;
(void) COMPILE_ERROR_IF_TRUE(SIZEOF_POOLBLOCK >
(SSIZE_T_MAX - POOL_MAX_ALLOC_SIZE));
if (pool == NULL)
i_fatal_status(FATAL_OUTOFMEM, "calloc(1, %zu): Out of memory",
SIZEOF_ALLOCFREE_POOL);
+ errno = old_errno;
#ifdef DEBUG
pool->name = strdup(name);
#endif
static void pool_allocfree_destroy(struct allocfree_pool *apool)
{
+ int old_errno = errno;
+
pool_allocfree_clear(&apool->pool);
if (apool->clean_frees)
safe_memset(apool, 0, SIZEOF_ALLOCFREE_POOL);
free(apool->name);
#endif
free(apool);
+ errno = old_errno;
}
static const char *pool_allocfree_get_name(pool_t pool ATTR_UNUSED)
{
struct allocfree_pool *apool =
container_of(pool, struct allocfree_pool, pool);
+ int old_errno = errno;
struct pool_block *block = calloc(1, SIZEOF_POOLBLOCK + size);
if (block == NULL)
i_fatal_status(FATAL_OUTOFMEM, "calloc(1, %zu): Out of memory",
SIZEOF_POOLBLOCK + size);
+ errno = old_errno;
block->size = size;
return pool_block_attach(apool, block);
}
{
struct allocfree_pool *apool =
container_of(pool, struct allocfree_pool, pool);
+ int old_errno = errno;
struct pool_block *block = pool_block_detach(apool, mem);
if (apool->clean_frees)
safe_memset(block, 0, SIZEOF_POOLBLOCK+block->size);
free(block);
+ errno = old_errno;
}
static void *pool_allocfree_realloc(pool_t pool, void *mem,
struct allocfree_pool *apool =
container_of(pool, struct allocfree_pool, pool);
unsigned char *new_mem;
+ int old_errno = errno;
struct pool_block *block = pool_block_detach(apool, mem);
if (old_size == SIZE_MAX)
if ((new_mem = realloc(block, SIZEOF_POOLBLOCK+new_size)) == NULL)
i_fatal_status(FATAL_OUTOFMEM, "realloc(block, %zu)",
SIZEOF_POOLBLOCK+new_size);
+ errno = old_errno;
/* zero out new memory */
if (new_size > old_size)
static void pool_alloconly_free_block(struct alloconly_pool *apool ATTR_UNUSED,
struct pool_block *block)
{
+ int old_errno = errno;
+
#ifdef DEBUG
safe_memset(block, CLEAR_CHR, SIZEOF_POOLBLOCK + block->size);
#else
}
#endif
free(block);
+ errno = old_errno;
}
static void
#endif
}
+ int old_errno = errno;
block = calloc(size, 1);
if (unlikely(block == NULL)) {
i_fatal_status(FATAL_OUTOFMEM, "block_alloc(%zu"
"): Out of memory", size);
}
+ errno = old_errno;
block->prev = apool->block;
apool->block = block;
static void *pool_system_malloc(pool_t pool ATTR_UNUSED, size_t size)
{
void *mem;
-#ifdef DEBUG
int old_errno = errno;
-#endif
mem = calloc(size, 1);
if (unlikely(mem == NULL)) {
i_fatal_status(FATAL_OUTOFMEM, "pool_system_malloc(%zu): "
"Out of memory", size);
}
-#ifdef DEBUG
- /* we rely on errno not changing. it shouldn't. */
- i_assert(errno == old_errno);
-#endif
+ errno = old_errno;
return mem;
}
void pool_system_free(pool_t pool ATTR_UNUSED, void *mem ATTR_UNUSED)
{
-#ifdef DEBUG
int old_errno = errno;
-#endif
+
#if defined(HAVE_MALLOC_USABLE_SIZE) && defined(DEBUG)
- safe_memset(mem, CLEAR_CHR, malloc_usable_size(mem));
+ if (mem != NULL)
+ safe_memset(mem, CLEAR_CHR, malloc_usable_size(mem));
#endif
free(mem);
-#ifdef DEBUG
- /* we rely on errno not changing. it shouldn't. */
- i_assert(errno == old_errno);
-#endif
+ errno = old_errno;
}
static void *pool_system_realloc(pool_t pool ATTR_UNUSED, void *mem,
old_size <= malloc_usable_size(mem));
#endif
+ int old_errno = errno;
mem = realloc(mem, new_size);
if (unlikely(mem == NULL)) {
i_fatal_status(FATAL_OUTOFMEM, "pool_system_realloc(%zu): "
"Out of memory", new_size);
}
+ errno = old_errno;
if (old_size < new_size) {
/* clear new data */